import axios from 'axios'
import qs from 'qs'
import store from '../store/index.js'

const JUHE_APPKEY = 'bb9f69b2c962ba2b6f88ce755a3391fc'

const axiosPost = options => {
	axios.post(
		options.url,
		qs.stringify({
			key: JUHE_APPKEY,
			...options.data
		})
	).then(res => {
		options.success(res)
		  console.log(res)
	}).catch(err => {
		options.error(err)
	})
}

const getWanNian = type => {
	return new Promise((resolve,reject) => {
		axiosPost({
			url: '/api/calendar/day',
			data: {
				date:type
			},
			success:function(data){
				resolve(data)
				// console.log(data)
			},
			error:function(err){
				reject(err)
			}
		})
	})
}

const getWanNianMonth = type => {
	return new Promise((resolve,reject) => {
		axiosPost({
			url: '/api/calendar/month',
			data: {
				'year-month':type
			},
			success:function(data){
				resolve(data)
				// console.log(data)
			},
			error:function(err){
				reject(err)
			}
		})
	})
}

const getWanNianYear = type => {
	return new Promise((resolve,reject) => {
		axiosPost({
			url: '/api/calendar/year',
			data: {
				year:type
			},
			success:function(data){
				resolve(data)
				// console.log(data)
			},
			error:function(err){
				reject(err)
			}
		})
	})
}

const getData = async (date,type) => {
	var data = null
	switch(date){
		case 'day':
			data = await getWanNian(type)
			store.state.dayList = data.data.result.data
			// console.log(store.state.dayList)
			break
		case 'month':
			data = await getWanNianMonth(type)
			store.state.monthList = data.data.result.data.holiday_array
			// console.log(store.state.monthList)
			break
		case 'year':
			data = await getWanNianYear(type)
			store.state.yearList = data.data.result.data.holidaylist
			// console.log(store.state.yearList)
			break
	}
}

export{
	getWanNian,
	axiosPost,
	getData
}